package com.dj.uphome.web.controller.system;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dj.common.web.controller.CoreCrudRestController;
import com.dj.common.web.rest.ResponsePack;
import com.dj.uphome.web.service.system.SysUserService;
import com.dj.uphome.web.vo.system.SysUserVo;

import io.swagger.annotations.Api;


/**
 * 类名称：系统用户
 */
@RestController
@RequestMapping(value = "/user")
@Api(value = "/user", description = "首页控制",hidden=true)
public class UserController extends CoreCrudRestController {
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 保存信息
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	/*@ApiOperation(value="保存用户", notes="")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "userName", value = "登录账号", required = true, dataType = "String"),
        @ApiImplicitParam(name = "password", value = "登录密码", required = true, dataType = "String"),
        @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String")
	})*/
	public ResponseEntity<ResponsePack<SysUserVo>> add(
			String userName,
			String password,
			String name) throws Exception {
		SysUserVo vo = new SysUserVo();
		vo.setUserName(userName);
		vo.setPassword(password);
		vo.setName(name);
		sysUserService.create(vo);
		return new ResponseEntity<>(new ResponsePack<>(vo), HttpStatus.OK);
	}
	@PostMapping(value = "/findByUserNameAndPassword")
	public ResponseEntity<ResponsePack<SysUserVo>> getUserByNameAndPwd(
			@RequestParam String userName, 
			@RequestParam String password) throws Exception {
		String passwd = new SimpleHash("SHA-1", userName, password).toString();
		return new ResponseEntity<>(new ResponsePack<>(sysUserService.getUserByNameAndPwd(userName, passwd)), HttpStatus.OK);
	}
	/**
	 * 删除用户
	 * 
	 * @param menuId
	 * @param out
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<SysUserVo> delete(@RequestParam int userId) throws Exception {
		sysUserService.delete(userId);
		return new ResponseEntity<>(HttpStatus.OK);
	}

	/**
	 * 显示用户列表(用户组)EasyuiPage
	 */
	@RequestMapping(value = "/listPage", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public ResponseEntity<ResponsePack<Page<SysUserVo>>> userPage(
			@PageableDefault(size = 10, sort = "user_id", direction = Sort.Direction.DESC) Pageable pageRequest) throws Exception {
		return new ResponseEntity<>(new ResponsePack<>(sysUserService.findPage(pageRequest, new SysUserVo())), HttpStatus.OK);
	}
}
